<?php
/**
 * User: 1971229701@qq.com
 * Date: 2020/11/6/006
 * Time: 10:27
 */

namespace app\index\logic;


use app\common\model\Cate;
use app\common\model\Video;
use app\common\model\VideoBanner;

class VideoLogic extends BaseLogic
{
    public function listing()
    {
        $video_banner = VideoBanner::where('state', 1)->select();
        foreach ($video_banner as &$value){
            $substr = substr($value['click_url'],0,4);
            if ($substr != "http"){
                $value['click_url'] = "";
            }
        }
        // 热门视频
        $hot_video = Video::where('type', 1)->order(['sort'=>'desc','id'=>'desc'])->limit(15)->select()->toArray();
//        $count = count($hot_video);
//        if ($count < 10){
//            $new_video = array_fill($count-1,10-$count,['image'=>'','id'=>'','title'=>'热门视频图片','desc'=>'']);
//            $hot_video = array_merge($hot_video,$new_video);
//        }
        // 私人高级定制视频
        $private_video = Video::where('type', 2)->limit(8)->select();
        // 原创
        $original_video = Video::where('type', 3)->limit(8)->select();

        return compact('video_banner', 'hot_video', 'private_video', 'original_video');
    }

    public function info($id)
    {
        // 增加阅读量
        Video::where('id',$id)->setInc('view');

        $info = Video::get($id);
        $info['type'] = Video::$my_type[$info['type']];
        $list = Video::limit(30)->select();

        return compact('info', 'list');
    }

    public function video_square($cate_id, $zuixin, $zuiduo, $haoche)
    {
        $where = [];
        $order = '';
        !empty($cate_id) && $where['pid'] = $cate_id;
        !empty($zuixin) && $order = 'create_time desc';
        !empty($zuiduo) && $order = 'view desc';
        !empty($haoche) && $where['type'] = 1;

        $cate_list = Cate::all();

        $query = Video::where($where);
        // 视频列表
        $video_list = $query->order($order)->paginate(9);
        $video_count = $query->count('id');

        // 右侧视频
        $right_video = Video::where('type', 1)->order('number', 'desc')->limit(6)->select();

        return compact('cate_list', 'video_list', 'right_video', 'video_count');
    }
}